<template>
  <div class="picture-waterfalll" ref="scrollContainer">
    <Waterfall :list="pictureVoList">
      <template #default="{ item }">
        <div class="waterfall-item">
          <a-tooltip color="#fff" placement="bottom">
            <template #title>
              <a-space>
                <a-tooltip>
                  <template #title>下载图片</template>
                  <a-button
                    :icon="h(DownloadOutlined)"
                    @click="goDownLoadImage(item.originalUrl as string, item.name as string)"
                  />
                </a-tooltip>
                <a-tooltip>
                  <template #title>上传到私人空间</template>
                  <a-button
                    :icon="h(UploadOutlined)"
                    @click="doAddPicture(item.originalUrl as string, item.name as string)"
                  ></a-button>
                </a-tooltip>
                <a-tooltip placement="bottom">
                  <h3 style="overflow: hidden; color: #666666; width: 50px; height: 25px">
                    {{ item.name }}
                  </h3>
                  <template #title>
                    {{ item.name }}
                  </template>
                </a-tooltip>
              </a-space>
            </template>
            <div class="image-card" @click="router.push(`/picture/detail/${item.id}`)">
              <div class="image-container">
                <LazyImg :url="item.thumbnailUrl" v-if="item.thumbnailUrl" class="lazy-img" />
                <div class="image-overlay">
                  <div class="image-title">{{ item.name }}</div>
                </div>
              </div>
            </div>
          </a-tooltip>
        </div>
      </template>
    </Waterfall>

    <div v-if="pictureVoList?.length === 0" class="empty-state">
      <a-spin v-if="loading" size="large" />
      <a-empty v-else description="暂无图片" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { LazyImg, Waterfall } from 'vue-waterfall-plugin-next'
import 'vue-waterfall-plugin-next/dist/style.css'
import { h, ref } from 'vue'
import { DownloadOutlined, UploadOutlined } from '@ant-design/icons-vue'
import { downloadImage } from '@/utils'
import { listSpaceVoByPageUsingPost } from '@/api/spaceController.ts'
import { SPACE_TYPE_ENUM } from '@/constants/space.ts'
import { useUserStore } from '@/stores/user.ts'
import { uploadPictureByUrlUsingPost } from '@/api/pictureController.ts'
import { useRouter } from 'vue-router'

// 下载图片
const goDownLoadImage = (url: string, fileName: string) => {
  downloadImage(url, fileName)
}

const pictureVoList = defineModel<API.PictureVO[]>()
const router = useRouter()
const loading = ref(false)

// 保存图片到我的空间
const userStore = useUserStore()

// 获取spaceId
const space = ref<API.SpaceVO>({})
const checkAuth = async () => {
  // 获取用户空间
  const res = await listSpaceVoByPageUsingPost({
    spaceType: SPACE_TYPE_ENUM.PRIVATE,
    userId: userStore?.userInfo?.id,
  })
  if (res.data.code === 0 && res.data.data?.records?.[0]) {
    space.value = res.data.data.records[0]
  }
}

// 上传图片url
const handleUpload = async (url: string, title: string) => {
  loading.value = true
  try {
    const res = await uploadPictureByUrlUsingPost({
      spaceId: space.value.id,
      fileUrl: url,
      picName: title,
    })
    if (res.data.code != 0) {
      return
    }
  } finally {
    loading.value = false
  }
}

// 加入我的空间
const doAddPicture = async (url: string, title: string) => {
  await checkAuth()
  await handleUpload(url, title)
  await router.push({
    path: '/space/my',
  })
}

// 滚动容器引用
const scrollContainer = ref<HTMLElement | null>(null)
</script>

<style lang="scss" scoped>
.picture-waterfalll {
  width: 100%;
  min-height: 200px;
  padding: 16px 0;
  position: relative;

  .waterfall-item {
    padding: 8px;
  }

  .image-card {
    overflow: hidden;
    border-radius: 16px;
    transition: all 0.3s ease;
    cursor: pointer;
    background: #fff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
    }

    .image-container {
      position: relative;
      overflow: hidden;
      width: 100%;
      min-height: 200px;
      max-height: 600px;

      .lazy-img {
        width: 100%;
        height: auto;
        object-fit: contain;
        transition: transform 0.4s ease;
      }

      .image-overlay {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: linear-gradient(to bottom, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.6));
        display: flex;
        align-items: flex-end;
        justify-content: center;
        padding-bottom: 20px;
        opacity: 0;
        transition: opacity 0.3s ease;
      }

      .image-title {
        color: white;
        font-size: 16px;
        text-align: center;
        padding: 10px 16px;
        width: 100%;
        transform: translateY(20px);
        transition: transform 0.3s ease;
        text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    &:hover {
      .lazy-img {
        transform: scale(1.05);
      }

      .image-overlay {
        opacity: 1;
      }

      .image-title {
        transform: translateY(0);
      }
    }
  }

  .empty-state {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 300px;
  }
}

// 响应式调整
@media (max-width: 768px) {
  .picture-waterfalll {
    .waterfall-item {
      padding: 6px;
    }

    .image-card {
      border-radius: 12px;
    }
  }
}
</style>
